const baseUrl = ""
//axios封装post请求
function postApi(url, data) {
    let result = axios({
        method: 'post',
        url: baseUrl + url,
        data: data,
        transformRequest: [function (data) {
            let ret = '';
            for (let i in data) {
                ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + "&";
            }
            return ret;
        }],
        header: {
            'Content-Type': 'application/json',
            // 'authorization': 'Bearer ' + getStorage('token'),
        }
    }).then(resp => {
        return resp.data;
    }).catch(error => {
        alert('接口错误!')
        return "exception=" + error;
    });
    return result;
}
//axios封装tokenpost请求
// function postTokenApi(url, data) {
//     let result = axios({
//         method: 'post',
//         url: baseUrl + url,
//         data: data,
//         // transformRequest: [function (data) {
//         //     let ret = '';
//         //     for (let i in data) {
//         //         ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + "&";
//         //     }
//         //     return ret;
//         // }],
//         headers: {
//             'Content-Type': 'application/json',
//             'authorization': 'Bearer ' + getStorage('token'),
//         }
//     }).then(resp => {
//         return resp.data;
//     }).catch(error => {
//         alert('接口错误!')
//         return "exception=" + error;
//     });
//     return result;
// }
//get请求
function getApi(url, data) {
    
    if (getStorage('token') === '' || getStorage('token') === null) {
        var result = axios({
            method: 'get',
            url: baseUrl + url,
            params: data,
            headers: {
                // 'authorization': 'Bearer ' + getStorage('token')||'',
            }
        }).then(function (resp) {
            return resp.data;
        }).catch(function (error) {
            return "exception=" + error;
        });
    }else{
        var result = axios({
            method: 'get',
            url: baseUrl + url,
            params: data,
            headers: {
                'authorization': 'Bearer ' + getStorage('token'),
            }
        }).then(function (resp) {
            return resp.data;
        }).catch(function (error) {
            return "exception=" + error;
        });
    }
  
    return result;
}
